# Usage: ps2eye.tcl game.ps game.txt

package require Iexpect

proc mention {error} {
	puts "Error: $error"
}

proc fileCheck {fileName fileExt hasToExist} {
	if {![string equal -nocase ".$fileExt" [file extension $fileName]]} {
		mention "Expected file extension $fileExt for file: $fileName"
		return 0
	}
	if {$hasToExist && ![file exists $fileName]} {
		mention "File does not exist: $fileName"
		return 0
	}
	return 1
}

if {![fileCheck [lindex $argv 0] ps 1]} return
if {![fileCheck [lindex $argv 1] txt 0]} return

if 0 {
# Parse info
iexpect::Expect I -file [lindex $argv 2] -white

# powers
set powers "UNOWNED"

I expect line "^-1$" {
} "^#" {
	I continue
} "^$" {
	I continue
} "^(\[A-Za-z\]+)$" {
	lappend powers [string toupper [I get match 1]]
	I continue
} default {
	mention "Unrecognized power: [I get output]"
	I continue
}

# territories
array set territories {}

I expect line "^-1$" {
} "^#" {
	I continue
} "^$" {
	I continue
} eof {
} "^(-?\[0-9\]+) (-?\[0-9\]+) \[|\](\[^|\]+)\[|\]" {
	set territories([list [I get match 1] [I get match 2]]) \
		[string toupper [I get match 3]]
	I continue
} default {
	mention "Unrecognized territory spec: [I get output]"
	I continue
}

itcl::delete object I
}

# Parse ps
set g [open [lindex $argv 1] w]

iexpect::Expect I -file [lindex $argv 0] -white

# Skip prolog
I expect line "^%%Page:" {
} default {
	I continue
}

# Piece identifications
set rePower "\[A-Z\]{2,}"
set rePoint "-?\[0-9\]+ -?\[0-9\]+"
set reLocation "\[A-Z\]{2,4}(/.C)?"
set reUnit "\[AF\] $reLocation" 

# Loop
I expect line "^%%Pages:" {
	puts Finished
} "^$" {
	I continue
} "^%" {
	puts "Comment: [I get output]"
	I continue
} "^Draw(Map|Names|NickNames)$" {
	I continue
} "^\[(\](\[^,\]+), Game Start\[)\] DrawTitle$" {
	puts $g "% Season\n"
	puts $g "([I get match 1]) GameStart"
	I continue
} "^\[(\](\[^,\]+), (After )?(Spring|Fall|Winter)( of)? (-?\[0-9\]+)\[)\] DrawTitle$" {
	puts $g "% Season\n"
	puts $g "([I get match 1]) [I get match 5] [string trimright [I get match 2]][I get match 3]"
	I continue
} "^$rePower$" {
	I continue
} "^$rePoint Draw(Army|Fleet)$" {
	I continue
} "^OrderReport$" {
	puts $g "\n% Orders"
	I continue
} "^\[(\]($rePower)\[)\] WriteOrder$" {
	puts $g "\n[I get match 1]\n"
	I continue
} "^\[(\] ?($reUnit)( H| \[SC\] $reLocation)?( -( $reLocation))?( \[A-Z \]+)?\[)\] WriteOrder$" {
	set result "!"
	switch [string trimleft [I get match 8]] "" {
	} "BOUNCE" {
		set result "|"
	} "DISLODGED" {
		set result "~"
	} "DESTROYED" {
		set result "*"
	} "CUT" {
		set result "|"
	} "NO CONVOY" {
		set result "?"
	} "VOID" {
		set result "?"
	} default {
		mention "Unrecognized result [string trimleft [I get match 8]]"
	}
	puts $g "[I get match 1][I get match 3][I get match 6] $result"
	I continue
} "^(FailedOrder )?($rePoint ){1,3}Arrow(Move|Hold|Support|Convoy|Retreat)( OkOrder)?$" {
	I continue
} "^RetreatReport$" {
	puts $g "\n% Retreats"
	set power {}
	I continue
} "^\[(\]($rePower) ($reUnit) - ($reLocation)( BOUNCE)?\[)\] WriteRetreat$" {
	if [string length [I get match 6]] {
		set result "*"
	} else {
		set result "!"
	}
	if {![string equal $power [I get match 1]]} {
		set power [I get match 1]
		puts $g "\n$power\n"
	}
	puts $g "[I get match 2] R [I get match 4] $result"
	I continue
} "^\[(\]($rePower) ($reUnit) DISBAND\[)\] WriteRetreat$" {
	if {![string equal $power [I get match 1]]} {
		set power [I get match 1]
		puts $g "\n$power\n"
	}
	puts $g "[I get match 2] R *"
	I continue
} "^AdjustReport$" {
	puts $g "\n% Adjustments"
	I continue
} "^\[(\]($rePower) BUILDS (\[^)\]+)\[)\] WriteAdjust$" {
	puts $g "\n[I get match 1]\n"
	foreach unit [split [I get match 2] ","] {
		set unit [string trimleft $unit]
		if [regexp $reUnit $unit] {
			puts $g "$unit +"
		} elseif {![string equal $unit "WAIVED"]} {
			mention "Unrecognized build $unit"
		}
	}
	I continue
} "^\[(\]($rePower) REMOVES (\[^)\]+)\[)\] WriteAdjust$" {
	puts $g "\n[I get match 1]\n"
	foreach unit [split [I get match 2] ","] {
		set unit [string trimleft $unit]
		if [regexp $reUnit $unit] {
			puts $g "$unit *"
		} else {
			mention "Unrecognized removal $unit"
		}
	}
	I continue
} "^OwnerReport$" {
	puts $g "\n% Centers"
	I continue
} "^\[(\]($rePower) ?\[(\]\[0-9\]+/\[0-9\]+\[)\] (\[^)\]+)\[)\] WriteOwner$" {
	puts $g "\n[I get match 1]\n"
	foreach center [I get match 2] {
		puts $g "$center $"
	}
	I continue
} "^\[(\]UNOWNED\[^)\]+\[)\] WriteOwner$" {
	I continue
} "^closepath newpath$" {
	I continue
} " supply$" {
	I continue
} "^Black$" {
	I continue
} "^ShowPage$" {
	puts $g "\n% Show\n\nShowPage\n"
	I continue
} default {
	mention "Unrecognized ps line: [I get output]"
	I continue
}

itcl::delete object I

close $g
